/**
 * Created by chenyong on 2015/7/23.
 */

var request = require('request'),
    config = require('./../config');


getToken: function fromHeaderOrQuerystring (req) {
    if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
        return req.headers.authorization.split(' ')[1];
    } else if (req.query && req.query.token) {
        return req.query.token;
    }
    return null;
}

//当前登录用户的信息
exports.currentuserinfo = function (req,res) {
    var userId = req.user._id
    //var userId = req.cookies.uid;
    var req_panoinfo = {
        url: config.api.rootpath + '/api/users/'+ userId,
        headers: {'Authorization': 'Bearer ' + config.api.token()}
    };
    request(req_panoinfo,function(err,httpResponse,body){

        if(!err)
        {
            var usrdata = JSON.parse(body);
            var userinfo = {
                _id: usrdata._id,
                nickname: usrdata.nickname
            };

            if(usrdata.hasOwnProperty('wechat'))
            {
                //userinfo.avatarurl = usrdata.wechat.headimgurl;
                userinfo.gendar = usrdata.wechat.sex;
            }
            if(usrdata.hasOwnProperty('avatarId') && usrdata.avatarId != null && usrdata.avatarId != "")
            {
                userinfo.avatarurl = "http://bpseast.blob.core.chinacloudapi.cn/uploads/useravatars/" + usrdata.avatarId + "/0"
            }
            else
            {
                userinfo.avatarurl = "http://cdn.bigpixel.cn/www/common/noavatar%4040x40.png"
            }
            return res.status(200).json(userinfo);
        }
        else
        {
            res.status(200).json({errcode:1,errmsg:"读取用户信息时出错"});
        }
    });
};